/*
 * This file is part of VLCJ.
 *
 * VLCJ is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VLCJ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VLCJ.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2009, 2010, 2011, 2012 Caprica Software Limited.
 */

package uk.co.caprica.vlcj.test.streaming;

import java.net.InetAddress;

import com.sun.jna.NativeLibrary;

import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.headless.HeadlessMediaPlayer;
import uk.co.caprica.vlcj.test.VlcjTest;

/**
 * An example of how to stream a media file over HTTP.
 * <p>
 * The client specifies an MRL of <code>http://127.0.0.1:5555</code>
 */
public class StreamHttp extends VlcjTest {
	
  public static void main(String[] args) throws Exception {
	  if(args.length != 1) {
	      System.out.println("Specify a single MRL to stream");
	      System.exit(1);
	    }
    if (System.getProperty("os.name").startsWith("Windows 7")||System.getProperty("os.name").startsWith("Windows 8"))
    	NativeLibrary.addSearchPath("libvlc", "C:\\Program Files\\VideoLAN\\VLC");
    else
    	NativeLibrary.addSearchPath("libvlc", "/usr/lib/");
    
    String localHost = InetAddress.getLocalHost().getHostName();
    String media = "v4l2:///dev/video0";//args[0];
    //String options = formatHttpStreamTranscode("192.168.0.100", 5555);
    String[] options = {
    	      ":sout=#transcode{vcodec=mp4v,vb=4096,scale=1,acodec=mpga,ab=128,channels=2,samplerate=44100}:duplicate{dst=std{access=http,mux=ts,dst=" + localHost + "}}",
    	      ":input-slave=alsa://hw:0,0"
    	    };

    System.out.println("Streaming '" + media + "' to '" + options + "'");

    MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(args);
    HeadlessMediaPlayer mediaPlayer = mediaPlayerFactory.newHeadlessMediaPlayer();
    mediaPlayer.playMedia(media, options);
   
    // Don't exit
    Thread.currentThread().join();
  }

  private static String formatHttpStream(String serverAddress, int serverPort) {
    StringBuilder sb = new StringBuilder(60);
    sb.append(":sout=#duplicate{dst=std{access=http,mux=ts,");
    sb.append("dst=");
    sb.append(serverAddress);
    sb.append(':');
    sb.append(serverPort);
    sb.append("}}:input-slave=alsa://hw:0,0");
    return sb.toString();
  }
  private static String formatHttpStreamTranscode(String serverAddress, int serverPort) {
	    StringBuilder sb = new StringBuilder(60);
	    sb.append(":sout=#transcode{vcodec=mp4v,acodec=mp4a,vb=800,ab=128,deinterlace}:standard{access=http,mux=ts,");
	    sb.append("dst=");
	    sb.append(serverAddress);
	    sb.append(':');
	    sb.append(serverPort);
	    sb.append("}");
	    return sb.toString();
	  }
}

